﻿using BCCommon;
using BCCommon.Enums;
using BCData.TMS.OrderTranstionRecord;
using BCDto.TMS.OrderTranstionRecord;
using BCEntity.TMS.OrderTranstionRecord;
using BCEntity.Common.EntityNameConst;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BCService.TMS.OrderTranstionRecord
{
	public class OrderTranstionRecordService : IOrderTranstionRecordService
	{
		private readonly IDatabaseContext databaseContext;
		private readonly IRedisService redisService;
		private readonly IOrderTranstionRecordData orderTranstionRecordData;
		private readonly string key = string.Format("{0}", EntityNameConst.OrderTranstionRecordEntity);

		public OrderTranstionRecordService(IDatabaseContext databaseContext,
			IRedisService redisService,
			IOrderTranstionRecordData orderTranstionRecordData)
		{
			this.databaseContext = databaseContext;
			this.redisService = redisService;
			this.orderTranstionRecordData = orderTranstionRecordData;
		}

		#region 检查订单流转记录是否存在
		public bool Exists(long orderTranstionRecordId)
		{
			return this.orderTranstionRecordData.Exists(orderTranstionRecordId).GetAwaiter().GetResult();
		}
		#endregion

		#region 添加订单流转记录
		public OrderTranstionRecordDto Add(OrderTranstionRecordRequestDto requestDto)
		{
			var entity = requestDto.As<OrderTranstionRecordEntity>();

			return this.orderTranstionRecordData.Add(entity).GetAwaiter().GetResult().As<OrderTranstionRecordDto>();
		}
		#endregion

		#region 修改订单流转记录
		public OrderTranstionRecordDto Update(OrderTranstionRecordPutDto putDto)
		{
			if (!this.Exists(putDto.OrderTranstionRecordId))
			{
				throw new ArgumentException("此记录不存在!");
			}
			var entity = putDto.As<OrderTranstionRecordEntity>();
			var result = this.orderTranstionRecordData.Update(entity).GetAwaiter().GetResult().As<OrderTranstionRecordDto>();
			string cacheKey = this.redisService.GetKey(key, entity.OrderTranstionRecordId);
			this.redisService.DeleteAsync(cacheKey);
			return result;
		}
		#endregion

		#region 删除订单流转记录
		public bool Delete(long orderTranstionRecordId)
		{
			if (!this.Exists(orderTranstionRecordId))
			{
				throw new ArgumentException("此记录不存在!");
			}

			var result = this.orderTranstionRecordData.Delete(orderTranstionRecordId).GetAwaiter().GetResult();
			string cacheKey = this.redisService.GetKey(key, orderTranstionRecordId);
			this.redisService.DeleteAsync(cacheKey);
			return result;
		}
		#endregion

		#region 获取订单流转记录
		public OrderTranstionRecordDto Get(long orderTranstionRecordId)
		{
			string cacheKey = this.redisService.GetKey(key, orderTranstionRecordId);
			var result = this.redisService.TryGet(cacheKey, () =>
			{
				var data = this.orderTranstionRecordData.GetEntity(orderTranstionRecordId).GetAwaiter().GetResult();
				return data;
			}, TimeSpan.FromDays(1));
			return result.As<OrderTranstionRecordDto>();
		}

		public OrderTranstionRecordDto GetProcessing(long orderId)
		{
			var result = this.orderTranstionRecordData.GetEntity(orderId).GetAwaiter().GetResult();
			return result.As<OrderTranstionRecordDto>();
		}
		#endregion

		#region 获取订单流转记录列表
		public IEnumerable<OrderTranstionRecordDto> GetList(long orderId)
		{
			string cacheKey = this.redisService.GetKey(key, orderId);
			var result = this.redisService.TryGet(cacheKey, () =>
			{
				return this.orderTranstionRecordData.GetList(orderId).GetAwaiter().GetResult();
			}, TimeSpan.FromDays(1));
			return result.As<IEnumerable<OrderTranstionRecordDto>>();
		}
		public IEnumerable<OrderTranstionRecordDto> GetList(string orderNo)
		{
			string cacheKey = this.redisService.GetKey(key, orderNo);
			var result = this.redisService.TryGet(cacheKey, () =>
			{
				return this.orderTranstionRecordData.GetList(orderNo).GetAwaiter().GetResult();
			}, TimeSpan.FromDays(1));
			return result.As<IEnumerable<OrderTranstionRecordDto>>();
		}
		#endregion

		#region 获取订单流转记录分页
		public PageableList<OrderTranstionRecordDto> Query(DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
		{
			string cacheKey = this.redisService.GetKey(key, start, end, sortName, ascending, pageIndex, pageSize);
			var result = this.redisService.TryGet(cacheKey, () =>
			{
				var data = this.orderTranstionRecordData.Query(start, end, sortName, ascending, pageIndex, pageSize).GetAwaiter().GetResult();
				return new PageableList<OrderTranstionRecordDto>
				{
					Count = data.Item2,
					Items = data.Item1.As<IEnumerable<OrderTranstionRecordDto>>(),
					PageIndex = pageIndex,
					PageSize = pageSize,
					ServerTime = DateTime.Now
				};
			}, TimeSpan.FromMinutes(30));

			return result;
		}
        #endregion
    }
}